function [features_struct]=calc_features_seg(image,HSV,seg_map, filters, norm_flag)

% This function calculates all features per segment - location, texture, color

% Function Inputs:
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% image - RGB image matrix
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% HSV - HSV image matrix
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% seg_map - map of segments
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% filters - filters to be used during calculating texture features. 
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% norm_flag - normalization flag.
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

% Function Outputs:
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% features_struct - 
%                    - seg_count           - number of segments
%                    - seg_loc_mat      -  matrix of location features of the segments
%                    - seg_text_mat   - matrix of texture features of the segments
%                    - seg_color_mat - matrix of color features of the segments
%                    - seg_indices      - cell that contains the indices of each segment
%                    - names_list         - cell of all features names' matrices 
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

% calculating general preliminary values
seg_count=length(setdiff(unique(seg_map),0));
names_list = cell(3, 1);
seg_indices=cell(1,seg_count);
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% calculating texture features - preliminary calculations
scales=size(filters,1);
orients=size(filters,2);
filter_num=scales*orients;
image_gray=rgb2gray(image);
texture_features = filter_num+4;
texture_mat=zeros(texture_features,seg_count);

% building all discription's labels
texture_names=cell(texture_features,1);
text='Texture feature - abs response for gabor filter - scale';
for i=1:filter_num
    scale_num=ceil(i/orients);
    temp=mod(i,orients);
    if (temp~=0)
        orient_num=temp;
    else orient_num=orients;
    end
    texture_names{i,1}=sprintf('%s %d orient %d',text,scale_num,orient_num);
end
texture_names{filter_num+1,1}='Texture feature - mean of variables from abs response';
texture_names{filter_num+2,1}='Texture feature - argmax  of variables from abs response';
texture_names{filter_num+3,1}='Texture feature - max-median  of variables from abs response';
texture_names{filter_num+4,1}='Texture feature - mean grayscale feature';

names_list{1} = texture_names;
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% calculating color features - preliminary calculations
%getting HSV image
color_features=6;
R=image(:,:,1);
G=image(:,:,2);
B=image(:,:,3);
H=HSV(:,:,1);
S=HSV(:,:,2);
V=HSV(:,:,3);
color_mat=zeros(color_features,seg_count);
% building all discription's labels
color_names=cell(6,1);
color_names{1,1}='Color feature - RGB values: mean of Red';
color_names{2,1}='Color feature - RGB values: mean of Green';
color_names{3,1}='Color feature - RGB values: mean of Blue';
color_names{4,1}='Color feature - HSV values: mean of Hue';
color_names{5,1}='Color feature - HSV values: mean of Saturation';
color_names{6,1}='Color feature - HSV values: mean of Value';

names_list{1} =  color_names;
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% calculating location features - preliminary calculations
location_features = 6;
height = size(seg_map,1);
width = size(seg_map,2);
map_size=size(seg_map);
location_mat=zeros(location_features,seg_count);

% building all discription's labels
location_names = cell(location_features,1);
location_names{1} = 'Location Feature - mean of normilized X';
location_names{2} = 'Location Feature - mean of normilized Y';
location_names{3} = 'Location Feature - 10th percentile of normilized X';
location_names{4} = 'Location Feature - 10th percentile of normilized Y';
location_names{5} = 'Location Feature - 90th percentile of normilized X';
location_names{6} = 'Location Feature - 90th percentile of normilized Y';

names_list{1} =  location_names;
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

h = waitbar(0,'Please wait... Calculating Segments Features');

for seg_num=1:seg_count
    indices=find(seg_map==seg_num);
    seg_indices{1,seg_num}=indices;
    waitbar(seg_num/seg_count)
    %-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    % calculating texture features - each seg
    for i=1:scales
        scale=(i-1)*orients;
        for j=1:orients
            texture_mat(scale+j,seg_num)=mean(filters{i,j}(indices));
        end
    end
    T1=texture_mat(1:filter_num,seg_num);
    texture_mat(filter_num+1,seg_num)=mean(T1);
    [maxval,maxarg]=max(T1);
    texture_mat(filter_num+2,seg_num)=maxarg;
    texture_mat(filter_num+3,seg_num)=maxval-median(T1);
    texture_mat(filter_num+4,seg_num)=mean(image_gray(indices));
    %-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    % calculating color features - each seg
    % for each segment, getting out all RGB and HSV values from original image
    rgb(:,1:3)=[R(indices) G(indices) B(indices)];
    hsv(:,1:3)=[H(indices) S(indices) V(indices)];
    % setting all means
    color_mat(1:3,seg_num)=mean(rgb(:,1:3));
    color_mat(4:6,seg_num)=mean(hsv(:,1:3));
    clear rgb;
    clear hsv;
    %-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    % calculating location features - each seg
     [y_coordinates, x_coordinates] = ind2sub(map_size,indices);
    location_mat(1,seg_num)= mean(x_coordinates)/width;
    location_mat(2,seg_num)= mean(y_coordinates)/height;
    location_mat(3,seg_num) = prctile(x_coordinates,10)/width;
    location_mat(4,seg_num) = prctile(y_coordinates,10)/height;
    location_mat(5,seg_num) = prctile(x_coordinates,90)/width;
    location_mat(6,seg_num) = prctile(y_coordinates,90)/height;
    %-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
end
close(h);

% setting the output
seg_text_mat=texture_mat;
seg_loc_mat=location_mat;
seg_color_mat=color_mat;

if (strcmp(norm_flag , 'true'))
    [seg_text_mat,seg_loc_mat,seg_color_mat] = normalize_data_seg(texture_mat,location_mat,color_mat);
end
features_struct = struct('seg_count',seg_count,'seg_loc_mat',seg_loc_mat,'seg_text_mat',seg_text_mat,'seg_color_mat',seg_color_mat,'names_list',{names_list},'seg_indices',{seg_indices});
